﻿using AutoMapper;

using B.S.Unit.Domain;
using B.S.Unit.ErrorCode;
using B.S.Unit.Infacet;
using B.S.Unit.Write.API.Applications.Command;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.Unit.Write.API.Applications.CommandHandler
{
    public class UnitAddCommandHandler : IRequestHandler<UnitAddCommand, UnitResult<object>>
    {
        private readonly IBaseRepository<UnitApp> purchaseRepo;
        private readonly IMapper mapper;

        public UnitAddCommandHandler(IBaseRepository<UnitApp> purchaseRepo,IMapper mapper)
        {
            this.purchaseRepo = purchaseRepo;
            this.mapper = mapper;
        }
        public Task<UnitResult<object>> Handle(UnitAddCommand request, CancellationToken cancellationToken)
        {
            UnitResult<object>result = new UnitResult<object>();

            result.Msg = UnitCode.添加成功.ToString();
            result.Code = UnitCode.添加成功;

            var entity = mapper.Map<UnitApp>(request);

            entity.UnitAppCode = YitIdHelper.NextId().ToString();

            entity.IsDeleted = false;

            purchaseRepo.Add(entity);

            result.Data = entity;

            return Task.FromResult(result);
        }
    }
}
